<template>
  <div>
    <div class="wrap">
      <div class="cates">
        <div class="cate-wrap">
          <div
            v-for="(cate,index) in cates"
            @click="changeCate(index)"
            :key="cate.id" :class="['cate',{
            active: index === num
          }]">
            {{cate.name}}
          </div>
        </div>
      </div>
      <div class="items">
        <div class="items-wrap">
          <div class="item" v-for="item in items" :key="item.id" @click="clickItem(item.id)">
            <img :src="item.pic" alt="">
            <div class="title">
              {{item.name}}
            </div>
            <div class="item-action">
              <div class="price">
                {{ item.minPrice | currency('￥') }}
              </div>
              <van-icon size="20" @click.stop="addCart(item)" class-prefix="icon" name="jia" />
            </div>
          </div>
        </div>
      </div>
      <my-tabbar />
    </div>
   </div>
</template>

<script>
import MyTabbar from '@components/MyTabbar'
import { fetchCates, fetchItems } from '@api'
import { currency } from '../../filters/index.js'
export default {
  data () {
    return {
      cates: [],
      items: [],
      num: 0
    }
  },
  created () {
    this.fetchCates()
  },
  methods: {
    clickItem (id) {
      // 进入详情
      this.$router.push({
        path: '/detail',
        query: {
          itemId: id
        }
      })
    },
    changeCate (index) {
      // 点击切换分类
      this.num = index
      this.fetchItems()
    },
    fetchItems () {
      this.items = []
      fetchItems({
        categoryId: this.cates[this.num].id
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    },
    fetchCates () {
      fetchCates().then(res => {
        console.log(res)
        if (res.data.code === 0) {
          this.cates = res.data.data
          this.fetchItems()
        }
      })
    }
  },
  filters: {
    currency
  },
  components: {
    MyTabbar
  }
}
</script>

<style lang="scss" scoped>
  .wrap {
    position: fixed;
    left: 0;
    right: 0;
    top: 0;
    bottom: 50px;

    display: flex;

    .cates {
      width: 30%;
      height: 100%;
      border-right: 1px solid #999;
      background-color: #eee;

      .cate {
        line-height: 40px;
        text-align: center;
        color: #333;
        font-size: 14px;

        &.active {
          background-color: #fff;
          border-left: 2px solid #e00;
        }
      }
    }

    .items {
      width: 70%;
      height: 100%;
      overflow: auto;
    }
  }

  .items-wrap {
    display: flex;
    flex-wrap: wrap;

    .item {
      width: 50%;
      display: flex;
      flex-direction: column;
      align-items: center;
      padding: 0 5px;
      box-sizing: border-box;

      img {
        width: 100%;
      }

      .title {
        width: 100%;
        line-height: 30px;
        color: #333;
        font-size: 14px;
        font-weight: 900;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }

      &-action {
        width: 100%;
        height: 30px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        color: #f66;
      }
    }
  }
</style>
